#/bin/bash


set -Eeo pipefail
echo $CMSHOSTS
declare CMSHOSTS=${CMSHOSTS:-""}
declare CMSNAMES=${CMSNAMES:-""}
echo $CMSHOSTS
exit
cd /opt
# deploy type can be hybrid, semi-hybrid, split
DEPLOYTYPE=${1:-hybrid}
export GS_PASSWORD='Gauss!23'
#export STANDBYHOSTS=127.0.0.1

export GAUSSHOME=/opt/openGauss
export GAUSSLOG=/opt/data/gausslog
export PGDATA=/opt/data/openGauss
export PGHOST=/opt/data/tmp
export GAUSS_CM_DIR=/opt/data/openGauss/cm


function generate_xml() {
    export PRIMARYHOST="172.11.0.2"
    export STANDBYHOSTS="172.11.0.3,172.11.0.4"
    export PRIMARYNAME=db-opengauss-01
    export STANDBYNAMES=db-opengauss-02,db-opengauss-03
    generate_conf
    echo "python3 generateconf.py --primary-host=${PRIMARYHOST} --standby-host=${STANDBYHOSTS} --primary-hostname=${PRIMARYNAME} --standby-hostname=${STANDBYNAMES}"
    python3 generateconf.py \
        --primary-host=${PRIMARYHOST} \
        --standby-host=${STANDBYHOSTS} \
        --primary-hostname=${PRIMARYNAME} \
        --standby-hostname=${STANDBYNAMES}
    #cat cluster.xml
}

function generate_semi_xml() {
    export PRIMARYHOST=172.11.0.4
    export STANDBYHOSTS="172.11.0.5,172.11.0.6"
    export PRIMARYNAME=db-opengauss-03
    export STANDBYNAMES=db-opengauss-04,db-opengauss-05
    export CMSHOSTS="172.11.0.2,172.11.0.3,172.11.0.4"
    export CMSNAMES=db-opengauss-01,db-opengauss-02,db-opengauss-03
    generate_conf
    echo "python3 generateconf.py --primary-host=${PRIMARYHOST} --standby-host=${STANDBYHOSTS} --primary-hostname=${PRIMARYNAME} --standby-hostname=${STANDBYNAMES}"
    python3 generateconf.py \
        --primary-host=${PRIMARYHOST} \
        --standby-host=${STANDBYHOSTS} \
        --primary-hostname=${PRIMARYNAME} \
        --standby-hostname=${STANDBYNAMES} \
        --cms-host=${CMSHOSTS} \
        --cms-hostname=${CMSNAMES} 
}
function generate_split_xml() {
    export PRIMARYHOST=172.11.0.5
    export STANDBYHOSTS="172.11.0.6,172.11.0.7"
    export PRIMARYNAME=db-opengauss-04
    export STANDBYNAMES=db-opengauss-05,db-opengauss-06
    export CMSHOSTS="172.11.0.2,172.11.0.3,172.11.0.4"
    export CMSNAMES=db-opengauss-01,db-opengauss-02,db-opengauss-03
    echo "python3 generateconf.py --primary-host=${PRIMARYHOST} --standby-host=${STANDBYHOSTS} --primary-hostname=${PRIMARYNAME} --standby-hostname=${STANDBYNAMES}"
    python3 generateconf.py \
        --primary-host=${PRIMARYHOST} \
        --standby-host=${STANDBYHOSTS} \
        --primary-hostname=${PRIMARYNAME} \
        --standby-hostname=${STANDBYNAMES} \
        --cms-host=${CMSHOSTS} \
        --cms-hostname=${CMSNAMES} 
}

function generate_conf() {
    # To-do: 
    # 1. support cms nodes ENV variables
    # 2. support check python3 module(done)
    if [[ $(pip3 show Jinja2 | wc -l)  -eq 10 ]];then
        echo "Find python module Jinja2, start to generate cluster.xml"
    else
        echo "Not find python module Jinja2, exit..."
        exit 1
    fi
    echo "cms hosts: |${CMSHOSTS}|"
    if [[ -n ${CMSHOSTS} ]] && [[ -n ${CMSNAMES} ]];then
        cat << EOF  
python3 /opt/generateconf.py --primary-host=${PRIMARYHOST} \
--primary-hostname=${PRIMARYNAME} \
--standby-host=${STANDBYHOSTS} \
--standby-hostname=${STANDBYNAMES} \
--cms-host=${CMSHOSTS} \
--cms-hostname=${CMSNAMES} 
EOF
        exit
        python3 /opt/generateconf.py --primary-host=${PRIMARYHOST} \
            --primary-hostname=${PRIMARYNAME} \
            --standby-host=${STANDBYHOSTS} \
            --standby-hostname=${STANDBYNAMES} \
            --cms-host=${CMSHOSTS} \
            --cms-hostname=${CMSNAMES} 
    else
        echo "python3 /opt/generateconf.py --primary-host=${PRIMARYHOST} --standby-host=${STANDBYHOSTS} --primary-hostname=${PRIMARYNAME} --standby-hostname=${STANDBYNAMES}"
        exit
        python3 /opt/generateconf.py --primary-host=${PRIMARYHOST} \
            --primary-hostname=${PRIMARYNAME} \
            --standby-host=${STANDBYHOSTS} \
            --standby-hostname=${STANDBYNAMES}
    fi
    if [ ! -f "${GAUSSDATA}/cluster.xml" ]; then
        echo "generate cluster xml file failed."
        exit 1
    fi
    echo "generate cluster xml file success."
}


echo "Deploy type: $DEPLOYTYPE"
if [[ $DEPLOYTYPE == "hybrid" ]];then
    generate_xml
elif [[ $DEPLOYTYPE == "semi-hybrid" ]];then
    generate_semi_xml
else
    generate_split_xml
fi